﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataModel;
using SchoolBusiness;

namespace SchoolAdministration
{
    public partial class AddStaffDetail : System.Web.UI.Page
    {
        private readonly BLFile _blFile = new BLFile();
        private readonly BlStaff _blstaff = new BlStaff();
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void StaffButton_Click(object sender, EventArgs e)
        {
            Staff staffdata = new Staff();
            staffdata.FirstName = TxtStaffFName.Text;
            staffdata.LastName = TxtStaffLName.Text;
            staffdata.MaritalStatus = MaritalStatus.Text;
            staffdata.Mobile = int.Parse(Mobile.Text);
            staffdata.Address = Address.Text;
            staffdata.Phone = int.Parse(Phone.Text);
            staffdata.dob = Convert.ToDateTime(DOB.Text);
            staffdata.CitizenNo = Citizenship.Text;
            staffdata.DateOfAppointment = Convert.ToDateTime(DateOfAppointment.Text);
            string FilePath = ConfigurationManager.AppSettings["FilePath"].ToString();
            bool blSucces = false;
            string filename = string.Empty;
            if (CVUpload.HasFile)
            {
                try
                {
                    string[] allowdFile = { ".pdf",".docx" };
                    //Here we are allowing only pdf file so verifying selected file pdf or not
                    string FileExt = System.IO.Path.GetExtension(CVUpload.PostedFile.FileName);
                    bool isValidFile = allowdFile.Contains(FileExt);
                    if (!isValidFile)
                    {
                        lblmsg.ForeColor = System.Drawing.Color.Red;
                        lblmsg.Text = "Please upload only pdf ";
                    }
                    else
                    {
                        // Get size of uploaded file, here restricting size of file
                        int FileSize = CVUpload.PostedFile.ContentLength;
                        if (FileSize <= 1048576)//1048576 byte = 1MB
                        {
                            FileData file = new FileData();
                            //Get file name of selected file
                            file.FileName = Path.GetFileName(CVUpload.FileName);
                            file.UserId = staffdata.StaffId;
                            //Save selected file into specified location
                            CVUpload.SaveAs(Server.MapPath(FilePath) + file.FileName);
                            file.FilePath = FilePath + file.FileName;
                            int result = _blstaff.SavaStaffDetail(staffdata, file); 
                            if (result > 0)
                            {
                                lblmsg.Text = "File upload successfully!";
                                blSucces = true;
                            }
                            
                        }
                        else
                        {
                            lblmsg.Text = "Attachment file size should not be greater then 1 MB!";
                        }
                    }
                }
                catch (Exception ex)
                {
                    lblmsg.Text = "Error occurred while uploading a file: " + ex.Message;
                }
            }
        }
    }
}